package com.codeoss.framework.view.parser;

import org.springframework.stereotype.Service;

import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.ExpressionUtil;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.FilterGroup;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

@Service("filtergroupParser")
public class FilterGroupParser extends AbstractComponentParser {
	@Override
	protected void setAttributes(IParseContext parseContext, ObjectNode objectNode, Component component) {
		super.setAttributes(parseContext, objectNode, component);
		FilterGroup filterGroup=(FilterGroup)component;
		
		ObjectNode propertyMapNode=(ObjectNode)objectNode.get("propertyMap");
		if(propertyMapNode!=null) {
			JsonNode jsonNode=propertyMapNode.get("dataSourceScript");
			if(jsonNode!=null) {
				String dataSourceScript=jsonNode.asText();
				if(!StringUtils.isEmpty(dataSourceScript)) {
					Expression expression=ExpressionUtil.convertToExpression(dataSourceScript, Expression.EXPRESSION_TYPE_JAVASCRIPT);
					filterGroup.setDataSourceScript(expression);
				}
			}
			
			jsonNode=propertyMapNode.get("onSelectedChangeExpression");
			if(jsonNode!=null) {
				String onSelectedChangeExpression=jsonNode.asText();
				if(!StringUtils.isEmpty(onSelectedChangeExpression)) {
					String expressionType=Expression.EXPRESSION_TYPE_JAVASCRIPT;
					if(onSelectedChangeExpression.startsWith("@")) {
						expressionType=Expression.EXPRESSION_TYPE_SPRING;
					}
					Expression expression=ExpressionUtil.convertToExpression(onSelectedChangeExpression,expressionType);
					filterGroup.setOnSelectedChangeExpression(expression);
				}
			}
		}
	}
	
	@Override
	protected Component createComponent(IParseContext parseContext, ObjectNode objectNode) {
		// TODO Auto-generated method stub
		return new FilterGroup();
	}

}
